package com.fsxgt.datagrid.console.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.fsxgt.datagrid.console.service.impl.ServerService;
import com.fsxgt.datagrid.core.exception.DataGridException;
import com.fsxgt.datagrid.core.utils.ReturnT;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequestMapping(produces = "application/json; charset=UTF-8",path = "/server")
@RestController
public class ServerController extends BaseController {

	@Autowired
	private ServerService serverService;
	
	


	
	@RequestMapping(path = "/register", method = RequestMethod.POST)
	public ReturnT register(@RequestBody JSONObject param, HttpServletRequest request) throws DataGridException {


		serverService.register(request.getRemoteHost(),param.getIntValue("port"));
		
		log.info(request.getRemoteHost()+":"+param.getIntValue("port"));

		return new ReturnT();

	}
	
	@RequestMapping(path = "/getServerList", method = RequestMethod.POST)
	public ReturnT getServerList(@RequestBody JSONObject param, HttpServletRequest request) throws DataGridException {

		Map result = new HashMap();
		result.put("code", "200");


		Set<String> list = serverService.getServerList();

		return new ReturnT(list);

	}
	
	
	


	

}